import 'dart:convert';

import 'package:commons/commons.dart';
import 'package:commons/src/core/aes/rsa.dart';
import 'package:commons/src/global/user/state.dart';
import 'package:flutter/foundation.dart';

extension Encryption on UserController {
  Future<void> getEncryption(String code, String privateKey) async {
    final priviteWrit = code.rsaDecryptWithPrivateKey(privateKey);
    try {
      final data = jsonDecode(priviteWrit!) as Map<String, dynamic>;
      final rsa = RSAModel.fromJson(data);
      state.rsa = rsa;
      final args = {
        'from': AppConfig.from,
        'uid': state.user?.uid ?? '',
        'utoken': state.token ?? '',
        'appid': AppConfig.appId,
      };
      final str = jsonEncode(args);
      state.aesData = str.encrypt();
    } catch (e) {
      debugPrint('aesData 解密错误: $e');
    }
  }
}
